<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.soa.workflow.request.MainTableInfo" %>
<%@ page import="weaver.soa.workflow.request.Property" %>
<%@ page import="weaver.soa.workflow.request.RequestInfo" %>
<%@ page import="weaver.soa.workflow.request.RequestService" %>
<%@ page import="weaver.workflow.request.RequestComInfo" %>
<%@ page import="weaver.workflow.workflow.WorkflowComInfo" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%--发送不及格重考试卷--%>
<%
    JSONObject jsonData = new JSONObject();
    String id = Util.null2String(request.getParameter("id"));
    String curdate = DateUtil.getCurrentDate();
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    BaseBean baseBean = new BaseBean();
    try {
        baseBean.writeLog(">>>>>againTest_Ajax.jsp>>>id=" + id);
        ResourceComInfo comInfo = new ResourceComInfo();
        rs.executeQuery("select * from uf_kstjtz where id=?", id);
        rs.next();
        String lcck = Util.null2String(rs.getString("lcck"));
        String cjr = Util.null2String(rs.getString("cjr"));
        String cjrq = Util.null2String(rs.getString("cjrq"));
        String sjmcwb = Util.null2String(rs.getString("sjmcwb"));
        String ktszlx = Util.null2String(rs.getString("ktszlx"));
        String yjr = Util.null2String(rs.getString("yjr"));
        String kssc = Util.null2String(rs.getString("kssc"));
        String kskssj = DateUtil.getFullDate().substring(0, 16);
        String zkjssj = Util.null2String(rs.getString("zkjssj"));
        String zf = Util.null2String(rs.getString("zf"));
        String jgfs = Util.null2String(rs.getString("jgfs"));
        String cjksry = Util.null2String(rs.getString("cjksry"));
        rs.executeQuery("select * from uf_kstjtz_dt1 where mainid=" + id + " and kszt=3 and sjdf<" + jgfs);
        int counts = rs.getCounts();
        baseBean.writeLog(">>>>>againTest_Ajax.jsp>>>counts=" + counts);
        while (rs.next()) {
            String mxid = Util.null2String(rs.getString("id"));
            String dtr = Util.null2String(rs.getString("dtr"));
            String username = comInfo.getLastname(dtr);
            String requestname = "在线考试答题-" + username + "-" + curdate;
            Map<String, String> requestMainMap = new HashMap<>();
            requestMainMap.put("dtr", dtr);
            requestMainMap.put("cjr", cjr);
            requestMainMap.put("gs", comInfo.getSubCompanyID(dtr));
            requestMainMap.put("bm", comInfo.getDepartmentID(dtr));
            requestMainMap.put("cjrq", cjrq);
            requestMainMap.put("sjmc", sjmcwb);
            requestMainMap.put("ktszlx", ktszlx);
            if (!"".equals(yjr)) {
                requestMainMap.put("yjr", yjr);
            }
            requestMainMap.put("kssc", kssc);
            requestMainMap.put("kskssj", kskssj);
            requestMainMap.put("ksjssj", zkjssj);
            requestMainMap.put("zf", zf);
            requestMainMap.put("jgfs", jgfs);
            requestMainMap.put("cjksry", cjksry);
            requestMainMap.put("jmsjid", id);
            requestMainMap.put("kslx", "1");
            requestMainMap.put("sjsplc", lcck);
            requestMainMap.put("kszt", "0");
            requestMainMap.put("sfzk", "0");
            String requestid = WorkflowCreateByRequestMap(dtr, "133", requestname, "1", requestMainMap);
            baseBean.writeLog(">>>requestid=" + requestid);
            if (Util.getIntValue(requestid) > 0) {
                String mainid = getIdByRequestid(requestid);
                String modeid = getIdToRequestid(lcck, "uf_sjtz");
                String[] strs = new String[]{"1", "2", "3", "4"};
                for (int j = 0; j < strs.length; j++) {
                    rs1.executeQuery("select tm,tmwb,fz,tmzf,tmxxnr,ROW_NUMBER() over(order by px) px from uf_sjtz_dt" + strs[j] + " where mainid=? order by px", modeid);
                    while (rs1.next()) {
                        String tm = Util.null2String(rs1.getString("tm"));
                        String fz = Util.null2String(rs1.getString("fz"));
                        String px = Util.null2String(rs1.getString("px"));
                        String tmwb = Util.null2String(rs1.getString("tmwb"));
                        String tmxxnr = Util.null2String(rs1.getString("tmxxnr"));
                        String tmzf = Util.null2String(rs1.getString("tmzf"));
                        rs1.executeUpdate("insert into formtable_main_286_dt" + strs[j] + " (mainid,tm,fz,px,tmwb,tmzf,tmxxnr) values(?,?,?,?,?,?,?)", mainid, tm, fz, px,tmwb,tmzf,tmxxnr);
                    }
                }
                //触发后流程后，台账中（考试状态）改为未答卷，清空（开始答卷时间）、（答卷结束时间）、（答题时长）、（试卷得分），（是否重考）改为是；
                rs.executeUpdate("update uf_kstjtz_dt1 set kszt=0,ksdjsj='',sjtjsj='',dtscfz='',sjdf=0,sfzk=0,sfyfs=1,sjxq=? where mainid=? and id=?", requestid, id, mxid);
            }
        }

        jsonData.put("status", true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>

<%!
    BaseBean baseBean = new BaseBean();

    //创建流程方法
    public String WorkflowCreateByRequestMap(String creatorId, String workflowId, String requestName, String IsNextFlow, Map<String, String> requestMainMap) {
        baseBean.writeLog("-----创建流程传递的参数个数--------");
        baseBean.writeLog("creatorId:" + creatorId);
        baseBean.writeLog("workflowId:" + workflowId);
        baseBean.writeLog("requestName:" + requestName);
        baseBean.writeLog("IsNextFlow:" + IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建人Id
        requestInfo.setWorkflowid(workflowId);//工作流Id
        requestInfo.setDescription(requestName);//请求标题
        if ("0".equals(IsNextFlow)) {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Map.Entry<String, String> entry : requestMainMap.entrySet()) {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            baseBean.writeLog("\r\n---p=" + p + "---Key=" + entry.getKey() + "---Value=" + entry.getValue());
            p++;
        }
        baseBean.writeLog("-----创建流程传递的参数个数p=" + p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);
        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                baseBean.writeLog("=======" + e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif = new WorkflowComInfo();
            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:" + rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:" + rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:" + wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:" + rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:" + requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            baseBean.writeLog(sbf.toString());
        } catch (Exception e) {
            baseBean.writeLog("错误：" + e);
        }
        return requestid;
    }

    //requestid转ID
    public static String getIdToRequestid(String requestid, String tablename) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tablename + " where lcck=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    public static String getIdByRequestid(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from formtable_main_286 where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString("id"));
    }


%>




